//index.js
//获取应用实例
// 轮播图接口
import { Banners } from '../../utils/http'
// 九宫格接口
import { navBar } from '../../utils/http'
// 秒杀接口
import { goods } from '../../utils/http'
Page({
  data: {
    // 轮播图
    swiper: [],
    // 九宫格
    navBar: [],
    // 秒杀
    secondsKill: [],
    hotStyle:[],
    crazy:[],
    group:[],
    goodslist:[],
    hous:'',
    minu:'',
    sec:'',
    inpVal:'',
  },
  change(e){
    // console.log(e.detail.value)
    wx.navigateTo({
      url: `/pages/search/search?str=${e.detail.value}`,
    })
  },
  toCategory(e){
    const id = e.currentTarget.dataset.operation
    wx.reLaunch({
      url: `/pages/category/category?id=${id}`
    });
  },
  // 秒杀
  goods() {
    let secondsKill = [];
    let hotStyle = [];
    let crazy = [];
    let group = [];
    goods().then(res => {
      // console.log(res.data)
      res.data.forEach(item => {
        // 秒杀
        if (item.id == 679353) {
          secondsKill.push(item)
        }
        // 爆款
        if(item.id === 679347 || item.id === 679349){
          hotStyle.push(item)
          // console.log(item)
        }
        // 砍价
        if(item.id === 679350){
          crazy.push(item)
        }
        // 拼团
        if(item.id === 679351){
          group.push(item)
        }
      })
      this.setData({
        secondsKill,
        hotStyle,
        crazy,
        group,
        goodslist:res.data
      })
    })
  },
  // 轮播图
  swiper() {
    Banners().then(res => {
      // console.log(res.data.slice(3,6))
      this.setData({
        swiper: res.data.slice(3, 6)
      })
    })
  },
  // 九宫格接口
  navBar() {
    navBar().then(res => {
      this.setData({
        navBar: res.data
      })
    })
  },
  // 时间
  time(){
    let odrsTime = new Date('2020-12-16')
    let newsTime = new Date()
    let nowTime = odrsTime - newsTime
    let hours = parseInt(nowTime/1000/60/60)%24
    let minutis = parseInt(nowTime/1000/60)%60
    let seconds = parseInt(nowTime/1000)%60
    let hous = hours < 10 ? '0' +hours : hours 
    let minu = minutis < 10 ? '0' +minutis : minutis 
    let sec = seconds < 10 ? '0' +seconds : seconds 
    this.setData({
      hous,minu,sec
    })
},
  onLoad: function () {
    // 轮播图接口
    this.swiper()
    // 九宫格接口
    this.navBar()
    // 秒杀
    this.goods()
    // 时间
    setInterval(this.time,1000);
    // this.time()
  },

})
